package com.betfair.wrapper.types;

import demo.util.Display;
import demo.util.InflatedMarketPrices;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: markwilliams
 * Date: 10/19/13
 * Time: 3:39 PM
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "Runner", uniqueConstraints = {
        @UniqueConstraint(columnNames = "iRunnerId")})
public class Runner {
    int _id;
    InflatedMarketPrices.InflatedRunner _runner = new InflatedMarketPrices.InflatedRunner();
    MarketPrice _marketPrice = new MarketPrice();
    com.betfair.www.publicapi.types.exchange.v5.Runner _marketRunner = new com.betfair.www.publicapi.types.exchange.v5.Runner();
    Map<Integer, Price> backPrices = new HashMap<Integer, Price>();
    Map<Integer, Price> layPrices = new HashMap<Integer, Price>();

    public Runner() {
    }

    public Runner(InflatedMarketPrices.InflatedRunner runner) {
        this.setRunner(runner);
    }

    @Id
    @GeneratedValue(generator = "increment")
    @GenericGenerator(name = "increment", strategy = "increment")
    @Column(name = "iRunnerId")
    public int getId() {
        return _id;
    }

    public void setId(int id) {
        this._id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "iMarketPriceId", nullable = false)
    public MarketPrice getMarketPrice() {
        return _marketPrice;
    }

    public void setMarketPrice(MarketPrice marketPrice) {
        this._marketPrice = marketPrice;
    }

    public int getSelectionId() {
        return _runner.getSelectionId();
    }

    private void setSelectionId(int param) {
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "runner")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    public Set<Price> getBackPrices() {
        Set<Price> prices = new HashSet<Price>(backPrices.values());
        return prices;
    }

    private void setBackPrices(Set<Price> param) {
    }

    public double getReductionFactor() {
        return _runner.getReductionFactor();
    }

    private void setReductionFactor(double param) {
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "runner")
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE})
    public Set<Price> getLayPrices() {
        Set<Price> prices = new HashSet<Price>(layPrices.values());
        return prices;
    }

    private void setLayPrices(Set<Price> param) {
    }

    public double getTotalAmountMatched() {
        return _runner.getTotalAmountMatched();
    }

    private void setTotalAmountMatched(double param) {
    }

    public double getNearSPPrice() {
        return _runner.getNearSPPrice();
    }

    private void setNearSPPrice(double param) {
    }

    public int getOrderIndex() {
        return _runner.getOrderIndex();
    }

    private void setOrderIndex(int param) {
    }

    public double getActualSPPrice() {
        return _runner.getActualSPPrice();
    }

    private void setActualSPPrice(double param) {
    }

    public double getLastPriceMatched() {
        return _runner.getLastPriceMatched();
    }

    private void setLastPriceMatched(double param) {
    }

    public double getFarSPPrice() {
        return _runner.getFarSPPrice();
    }

    private void setFarSPPrice(double param) {
    }

    public double getHandicap() {
        return _runner.getHandicap();
    }

    private void setHandicap(double param) {
    }

    public boolean isVacant() {
        return _runner.isVacant();
    }

    private void setVacant(boolean param) {
    }

    @Transient
    public com.betfair.www.publicapi.types.exchange.v5.Runner getMarketRunner() {
        return _marketRunner;
    }

    public void setMarketRunner(com.betfair.www.publicapi.types.exchange.v5.Runner marketRunner) {
        if (marketRunner == null) {
            marketRunner = new com.betfair.www.publicapi.types.exchange.v5.Runner();
        }
        this._marketRunner = marketRunner;
    }

    public String getName() {
        return _marketRunner.getName();
    }

    public void setName(String param) {
        _marketRunner.setName(param);
    }

    public int getAsianLineId() {
        return _marketRunner.getAsianLineId();
    }

    public void setAsianLineId(int param) {
        _marketRunner.setAsianLineId(param);
    }

    public void setRunner(InflatedMarketPrices.InflatedRunner runner) {
        this._runner = runner;

        for (InflatedMarketPrices.InflatedPrice inflatedPrice : _runner.getBackPrices()) {
            Price price = new Price(inflatedPrice);
            price.setPriceType("B");
            price.setRunner(this);
            backPrices.put(price.getDepth(), price);
        }
        for (InflatedMarketPrices.InflatedPrice inflatedPrice : _runner.getLayPrices()) {
            Price price = new Price(inflatedPrice);
            price.setPriceType("L");
            price.setRunner(this);
            layPrices.put(price.getDepth(), price);
        }

    }

    private void setupBackPrices() {
        backPrices = new HashMap<Integer, Price>();
        for (InflatedMarketPrices.InflatedPrice inflatedPrice : _runner.getBackPrices()) {
            Price price = new Price(inflatedPrice);
            price.setPriceType("B");
            price.setRunner(this);
            backPrices.put(price.getDepth(), price);
        }
    }

    private void setupLayPrices() {
        layPrices = new HashMap<Integer, Price>();
        for (InflatedMarketPrices.InflatedPrice inflatedPrice : _runner.getLayPrices()) {
            Price price = new Price(inflatedPrice);
            price.setPriceType("L");
            price.setRunner(this);
            layPrices.put(price.getDepth(), price);
        }
    }
}
